<?php

namespace app\api\controller;

use app\api\controller\Base;
use app\common\model\Article;
use app\common\model\Collect;
use app\common\model\Zan;
use app\common\model\Comment;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Index extends Base
{
    protected $noNeedLogin = ['index','article','addcollect','delcollect','addzan','addcomment','mycollect'];
    protected $noNeedRight = ['*'];


    public function other(){
        $list = [
            [
                'logo'=>"https://jizhang.lishiketang.com/image/xiaochengxu/sccy.jpg",
                'name'=>"诗词成语大全",
                'desc'=>"中华古典诗词是我们祖国文化的瑰宝，是中华民族智慧的结晶。诗词成语大全让人们在娱乐中学习中华瑰宝",
                'appid'=>"wxc718f300e8a0c723",
            ]
        ];
        $this->success('成功！',['show'=>false]);
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $param = $this->request->param();
        $currentPage = isset($param['page'])?(int)$param['page']:1;
        $perPage = isset($param['per_page'])?(int)$param['per_page']:10;

        $offset = ($currentPage-1) * $perPage;

        $articleModel = model("Article");
        $rs = collection($articleModel->field('image,num,title,id,source')->where(['status'=>1])->order('zan desc,id desc')->limit($offset,$perPage)->select())->toArray();
        // echo $articleModel->getLastSql();exit;
        if(!empty($rs)){
            
            foreach($rs as $k=>$v){
                $rs[$k]['image'] = $this->getImgUrl($v['image'],$v['source']);
            }
            $this->success('成功!',$rs);
        } else {
            $this->error('无数据');
        }
    }
    
    private function getImgUrl($imgUrl,$source){
        // print_r($arr);exit;
        $domain = Config::get('domain');
        if($source == 2){
            return $domain.$imgUrl;
        }
        return $imgUrl;
    }

    public function article(){
        $param = $this->request->param();
        $id = isset($param['id'])?(int)$param['id']:0;
        if(empty($id)){
            $this->error('参数不能为空！');
        }
        $articleModel = model("Article");
        $info = $articleModel->where(['id'=>$id])->find();
        if(!empty($info['id'])){
            $imageArr = json_decode($info['images'],true);
            // $domain = Config::get('domain');
            if(!empty($imageArr)){
                foreach($imageArr as $k=>$v){
                    $imageArr[$k] = $this->getImgUrl($v,$info['source']);
                }
            }

            $this->success('成功！',$imageArr);
        } else {
            $this->error('图片不存在！');
        }
    }

    public function addcollect(){
        $param = $this->request->param();
        $aid = isset($param['aid'])?(int)$param['aid']:0;
        if(empty($aid)){
            $this->error('请选择收藏的图片！');
        }
        $userId = $this->chckedIsLogin($param);

        $articleModel = model('Article');
        $articleInfo = $articleModel->where(['id'=>$aid])->find();
        if(empty($articleInfo['id'])){
            $this->error('您要收藏的图片不存在！');
        }
        $collectModel = model('Collect');
        $info = $collectModel->where(['uid'=>$userId,'aid'=>$aid])->find();
        if(!empty($info['id'])){
            $this->error('已收藏！','',2);
        }

        $saveArr = [
          'aid'=>$aid,
          'uid' => $userId,
        ];
        $rs = $collectModel->save($saveArr);
        $articleModel->where(['id'=>$aid])->setInc('collect');
        if(!empty($rs)){
            $this->success('收藏成功！');
        } else {
            $this->error('收藏失败！');
        }
    }

    public function delcollect(){
        $param = $this->request->param();
        $aid = isset($param['aid'])?(int)$param['aid']:0;
        if(empty($aid)){
            $this->error('请选择要取消收藏的图片！');
        }
        $userId = $this->chckedIsLogin($param);

        $articleModel = model('Article');
        $articleInfo = $articleModel->where(['id'=>$aid])->find();
        if(empty($articleInfo['id'])){
            $this->error('您要取消收藏的图片不存在！');
        }


        $collectModel = model('Collect');

        $info = $collectModel->where(['uid'=>$userId,'aid'=>$aid])->find();
        if(empty($info['id'])){
            $this->error('您还未收藏此图，不用取消！');
        }

        $saveArr = [
            'aid'=>$aid,
            'uid' => $userId,
        ];
        $rs = $collectModel->where($saveArr)->delete();
        $articleModel->where(['id'=>$aid])->setDec('collect');
        if(!empty($rs)){
            $this->success('取消成功！');
        } else {
            $this->error('取消失败！');
        }
    }

    public function addzan(){
        $param = $this->request->param();
        $aid = isset($param['aid'])?(int)$param['aid']:0;
        if(empty($aid)){
            $this->error('请选择要点赞的图片！');
        }
        $userId = $this->chckedIsLogin($param);

        $articleModel = model('Article');
        $articleInfo = $articleModel->where(['id'=>$aid])->find();
        if(empty($articleInfo['id'])){
            $this->error('您要点赞的图片不存在！');
        }

        $zanModel = model('Zan');

        $info = $zanModel->where(['uid'=>$userId,'aid'=>$aid])->find();
        if(!empty($info['id'])){
            $this->error('您已赞过此图！','',2);
        }

        $saveArr = [
            'aid'=>$aid,
            'uid' => $userId,
        ];
        $rs = $zanModel->save($saveArr);
        $articleModel->where(['id'=>$aid])->setInc('zan');
        if(!empty($rs)){
            $this->success('点赞成功！');
        } else {
            $this->error('点赞失败！');
        }


    }

    public function addcomment(){
        $param = $this->request->param();
        $aid = isset($param['aid'])?(int)$param['aid']:0;
        $content = isset($param['content'])?$param['content']:'';
        if(empty($aid)){
            $this->error('请选择要评论的图片！');
        }
        if(empty($content)){
            $this->error('评论能不能为空！');
        }
        $userId = $this->chckedIsLogin($param);

        $articleModel = model('Article');
        $articleInfo = $articleModel->where(['id'=>$aid])->find();
        if(empty($articleInfo['id'])){
            $this->error('您要评论的图片不存在！');
        }

        $commentModel = model("comment");
        $saveArr = [
            'aid'=>$aid,
            'uid' => $userId,
            'content' => $content,
        ];
        $rs = $commentModel->save($saveArr);
        model("Article")->where(['id'=>$aid])->setInc('comment');
        if(!empty($rs)){
            $this->success('评论添加成功！');
        } else {
            $this->error('评论添加失败！');
        }

    }
    public function mycollect(){
        $param = $this->request->param();
        $userId = $this->chckedIsLogin($param);
        
        $currentPage = isset($param['page'])?(int)$param['page']:1;
        $perPage = isset($param['per_page'])?(int)$param['per_page']:10;

        $offset = ($currentPage-1) * $perPage;
        

        $sql = "select a.image,a.id,a.num,a.title from mt_collect as c left join mt_article as a on c.aid=a.id where c.uid=? limit $offset,$perPage";
        // echo $sql;exit;
        $rs = Db::query($sql,[$userId]);
        if(!empty($rs)){
            $this->success('成功',$rs);
        } else {
            $this->error('暂无收藏数据');
        }
    }
}
